package com.dzsw.redis.controller;

import com.dzsw.redis.bean.User;
import com.dzsw.redis.service.IBizService;
import com.dzsw.redis.util.JsonUtil;
import com.dzsw.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController  {

    private static int ExpireTime = 60;   // redis中存储的过期时间60s

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private IBizService bizService;

    @RequestMapping("set")
    public String redisset(String key, String item){
        User u =new User();
        u.setId(Long.valueOf(1));
        u.setGuid(String.valueOf(1));
        u.setName("zhangsan");
        u.setAge(String.valueOf(20));
        u.setCreateTime(new Date());
        redisUtil.hSet(key, item, JsonUtil.toJson(u), 30);
        return "ok";
    }

    @RequestMapping("get")
    public Object redisget(String key){
        return redisUtil.get(key);
    }

    @RequestMapping("expire")
    public String expire(String key){
        redisUtil.expire(key,ExpireTime);
        return "ok";
    }

    @GetMapping("/test")
    public String test(String m) {
        String ret = bizService.handle(m);
        return ret;
    }

}
